﻿using MD5Hash;
using Model;

namespace Dal
{
    public class adminDal
    {
        private readonly MyDbContext db;

        public adminDal(MyDbContext db)
        {
            this.db = db;
        }

        /// <summary>
        /// 注册
        /// </summary>
        /// <param name="info"></param>
        /// <returns></returns>
        public int PostRegister(T_Admin info)
        {
            try
            {
                db.T_Admin.Add(info);
                return db.SaveChanges();
            }
            catch (Exception)
            {
                throw;
            }
        }

        /// <summary>
        /// 登录
        /// </summary>
        /// <param name="name"></param>
        /// <param name="pwd"></param>
        /// <returns></returns>
        public T_Admin GetLogin(string name,string pwd)
        {
            try
            {
                var list = db.T_Admin.Where(x=>x.Anmae == name && x.Apwd == pwd).FirstOrDefault();
                if(list == null)
                {
                    return new T_Admin() { Aid = -1 };
                }
                else
                {
                    if(list.Apwd == pwd.GetMD5())
                    {
                        return list;
                    }
                    else
                    {
                        return new T_Admin() { Aid = -2 };
                    }
                }
            }
            catch (Exception)
            {
                throw;
            }
        }


    }
}